//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModelDiscounter.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::CMarketModelDiscounter (Double paymentTime, Cephei::Core::IVector<Double>^ rateTimes) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phMarketModelDiscounter = NULL;
#endif
        QuantLib::Time _paymentTime = (QuantLib::Time)ValueHelper::Convert (paymentTime); //d
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        _ppMarketModelDiscounter = new boost::shared_ptr<QuantLib::MarketModelDiscounter> (new QuantLib::MarketModelDiscounter ( _paymentTime,  _rateTimes ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::CMarketModelDiscounter (boost::shared_ptr<QuantLib::MarketModelDiscounter>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelDiscounter = NULL;
#endif
	_ppMarketModelDiscounter = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::CMarketModelDiscounter (QuantLib::MarketModelDiscounter& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMarketModelDiscounter = NULL;
#endif
	_ppMarketModelDiscounter = new boost::shared_ptr<QuantLib::MarketModelDiscounter> (&childNative);
    
    _MarketModelDiscounterOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::CMarketModelDiscounter (CMarketModelDiscounter^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelDiscounter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelDiscounter = new boost::shared_ptr<QuantLib::MarketModelDiscounter> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::CMarketModelDiscounter (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelDiscounter = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelDiscounter::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::CMarketModelDiscounter (QuantLib::Handle<QuantLib::MarketModelDiscounter>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelDiscounter = &childNative;
	_ppMarketModelDiscounter = &static_cast<boost::shared_ptr<QuantLib::MarketModelDiscounter>>(childNative.currentLink());
    
    _MarketModelDiscounterOwner = owner;
}
Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::CMarketModelDiscounter (QuantLib::Handle<QuantLib::MarketModelDiscounter> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelDiscounter = &childNative;
	_ppMarketModelDiscounter = &static_cast<boost::shared_ptr<QuantLib::MarketModelDiscounter>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::CMarketModelDiscounter (QuantLib::MarketModelDiscounter childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelDiscounter = NULL;
#endif
	_ppMarketModelDiscounter = new boost::shared_ptr<QuantLib::MarketModelDiscounter> (new QuantLib::MarketModelDiscounter (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::~CMarketModelDiscounter ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMarketModelDiscounter != NULL)
    {
	    delete _ppMarketModelDiscounter;
        _ppMarketModelDiscounter = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::!CMarketModelDiscounter ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMarketModelDiscounter != NULL)
    {
	    delete _ppMarketModelDiscounter;
    }
}
QuantLib::MarketModelDiscounter& Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::GetReference ()
{
    if (_ppMarketModelDiscounter == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModelDiscounter;
}
boost::shared_ptr<QuantLib::MarketModelDiscounter>& Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::GetShared ()
{
    if (_ppMarketModelDiscounter == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModelDiscounter;
}
QuantLib::MarketModelDiscounter* Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::GetPointer ()
{
    if (_ppMarketModelDiscounter == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModelDiscounter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelDiscounter>& Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::GetHandle ()
{
	if (_phMarketModelDiscounter == NULL)
	{
		_phMarketModelDiscounter = new Handle<QuantLib::MarketModelDiscounter> (*_ppMarketModelDiscounter);
	}
	return *_phMarketModelDiscounter;
}
#endif
bool Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::HasNative () 
{
	return (_ppMarketModelDiscounter != NULL);
}

Double Cephei::QL::Models::Marketmodels::CMarketModelDiscounter::NumeraireBonds (Cephei::QL::Models::Marketmodels::ICurveState^ curveState, UInt64 numeraire)
{
    CCurveState^ _CcurveState;
    try
    {
        _CcurveState = safe_cast<CCurveState^> (curveState);
        _CcurveState->Lock();
        QuantLib::CurveState& _curveState = static_cast<QuantLib::CurveState&> (_CcurveState->GetReference ()); 
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMarketModelDiscounter)->numeraireBonds ( _curveState,  _numeraire );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurveState != nullptr) _CcurveState->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::IMarketModelDiscounter^ Cephei::QL::Models::Marketmodels::CMarketModelDiscounter_Factory::Create (Double paymentTime, Cephei::Core::IVector<Double>^ rateTimes)
{
    return REFNEW CMarketModelDiscounter ( paymentTime,  rateTimes);
}
